#include <iostream>
#include <iomanip>
#include "whose.h"
#include <io.h>
using namespace std;

Whose::Whose(char *targetName)
{
	path = getenv("PATH");
	pathext = getenv("PATHEXT");
	this->targetName = targetName;
}

char * Whose::getPath()
{
	return path;
}

char * Whose::getTargetName()
{
	return targetName;
}

void Whose::existOrNot()
{
	char *pointer,*start,*end;
	pointer = path;
	start = path;
	for(;*pointer!='\0';pointer++)
	{
		if(*pointer==';' || *(pointer+1)=='\0')
		{
			end = pointer - 1;
			char *finalPath = (char *)malloc(sizeof(char)*(pointer-start+1+strlen(targetName)));
			memset(finalPath,0,sizeof(char)*(pointer-start+1+strlen(targetName)));
			strncpy(finalPath,start,sizeof(char)*(pointer-start));
			if(*end!='\\')
				strcat(finalPath,"\\");

			strcat(finalPath,targetName);

			if(_access(finalPath,0) == 0)
				cout<<finalPath<<endl;

			start = pointer + 1;
		}
	}
}